import axios from "axios";
import { nanoid } from "nanoid";

// 人员管理相关的配置
const  personOptions = {
   // 开启命名空间，帮助 mapState,mapActions,mapMutations,mapGetters 方法获取 personOptions 中的值
  namespaced:true,
  actions: {
    addPersonWang(context, value){
        if(value.name.indexOf('wang') === 0){
          context.commit('ADD_PERSON', value)
        }else{
          alert("添加的人必须姓wang")
        }
    },
    addPersonServer(context){
        axios.get('https://api.uixsj.cn/hitokoto/get?type=social').then(
          res => {
            context.commit('ADD_PERSON', {id:nanoid, name: res.data})
          },
          error => {
            alert(error.message)
          }
        )
    }
  },
  mutations: {
    ADD_PERSON(state, value){
      console.log("mutations ADD_PERSON called");
      state.personList.unshift(value)
    }
  },
  state: {
    personList: [
      {id:'001',name:'zs'}
    ]
  },
  getters: {
    firstPersonName(state){
      return state.personList[0].name
    }
  },
}

export default personOptions